package com.gensdai.leliang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.gensdai.leliang.AppApplication;
import com.gensdai.leliang.R;
import com.gensdai.leliang.base.BaseActivity;
import com.gensdai.leliang.dialog.Upload;
import com.gensdai.leliang.dialog.dialog_select_pay_type_test;
import com.gensdai.leliang.dialog.dialog_setup_password;
import com.gensdai.leliang.entity.Address;
import com.gensdai.leliang.entity.parseBean.BaseParse;
import com.gensdai.leliang.entity.parseBean.OrderPreperBean;
import com.gensdai.leliang.entity.parseBean.ParentBean;
import com.gensdai.leliang.popupwindow.BasePopupWindow;
import com.gensdai.leliang.remoteInterface.ApiService;
import com.gensdai.leliang.remoteInterface.RetrofitFactory;
import com.gensdai.leliang.retrofitUtils.ModelFilteredFactory;
import com.gensdai.leliang.utils.BaseUtils;
import com.gensdai.leliang.utils.PreferencesUtils;
import com.gensdai.leliang.view.NoScrollListView;
import com.gensdai.leliang.view.Toaster;
import com.gensdai.leliang.zfb.PayResult;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.android.BuildConfig;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 订单待完成界面，准备开始下单页面
 * Created by Administrator on 2017/5/27.
 */

public class OrderPreperActivity extends BaseActivity {

    //获取地址回调code码
    public static final int GETADDRESSINFOCODE = 0x111;

    public static String prefreceName = "User";
    public static String prefreceKey = "hasPayPassword";
    public static String prefrece_userid = "user_id";
    public static String prefrece_userno = "userno";

    public static String prefrece_addressid = "addressId";
    public static String prefrece_addressname = "name";
    public static String prefrece_addressprovince = "province";
    public static String prefrece_adddressphone = "adressPhone";
    public static String prefrece_address = "detailAddress";

    public static final String EXTRA_PRODUCT = "PRODUCT";
    public static final String EXTRA_FROM_FLAG = "fromflag";
    @BindView(R.id.postBtn)
    Button postBtn;

    dialog_select_pay_type_test test;
    public boolean state;
    dialog_setup_password pass;

    List<OrderPreperBean> order;
    @BindView(R.id.preperOrderList)
    NoScrollListView preperOrderList;
    @BindView(R.id.countPrice)
    TextView countPrice;
    @BindView(R.id.bottomcountprice)
    TextView bottomcountprice;
    @BindView(R.id.addressUserInfoLayout)
    View addressUserInfoLayout;
    @BindView(R.id.nameLable)
    TextView nameLable;
    @BindView(R.id.phoneLable)
    TextView phoneLable;
    @BindView(R.id.addressNameLayout)
    RelativeLayout addressNameLayout;
    @BindView(R.id.detailAddress)
    TextView detailAddressText;
    @BindView(R.id.kuaidilable)
    TextView kuaidilable;
    @BindView(R.id.kuaidipricelable)
    TextView kuaidipricelable;


    private boolean hasPayPassword = false;
    private String userid = "";   /*000000*/
    private String orderid = "";/*29f6f92bf6da463aa15e670403aab7dc*/
    private String paytype = "yue";

    private String flag = "2";
    private String addressid = "";  /*000c52adb84047868ab28f76e614722e*/
    private String productAttributeId = "";//属性ID
    private String buyNum = "1";//购买数量


    //获取收货地址信息
    private String addressName = "";
    private String addressPhone = "";
    private String detailAddress = "";
    private String addressprovince = "";

    private StringBuffer scidList = null; //从购物车过来的订单集合 在本页面处理

    @Override
    protected String getTitleStr() {
        return "确认订单";
    }

    @Override
    protected boolean isShowBackButton() {
        return true;
    }

    @Override
    protected boolean isCustomTitle() {
        return false;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_ordere_done;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //配置沙箱环境
//        if (BuildConfig.DEBUG) {
//            EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
//        }

        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        order = (List<OrderPreperBean>) getIntent().getExtras().getSerializable(EXTRA_PRODUCT);
        flag = getIntent().getStringExtra(EXTRA_FROM_FLAG);
        hasPayPassword = PreferencesUtils.getBooleanPreference(this, prefreceName, prefreceKey, false); //获取是否设置过支付密码
        userid = PreferencesUtils.getStringPreference(this, prefreceName, prefrece_userid, ""); //获取用户id没有登陆就是没有

        addressid = PreferencesUtils.getStringPreference(this, prefreceName, prefrece_addressid, ""); //获取用户id没有登陆就是没有
        addressName = PreferencesUtils.getStringPreference(this, prefreceName, prefrece_addressname, ""); //获取用户id没有登陆就是没有
        addressprovince = PreferencesUtils.getStringPreference(this, prefreceName, prefrece_addressprovince, ""); //获取用户id没有登陆就是没有
        detailAddress = PreferencesUtils.getStringPreference(this, prefreceName, prefrece_address, ""); //获取用户id没有登陆就是没有
        addressPhone = PreferencesUtils.getStringPreference(this, prefreceName, prefrece_adddressphone, ""); //获取用户id没有登陆就是没有
        addressprovince = PreferencesUtils.getStringPreference(this, prefreceName, prefrece_addressprovince, ""); //


        ButterKnife.bind(this);
        up = new Upload(this);
        //初始化收获信息
        initAddressView();
        //从购物车过来的 1
        if (TextUtils.equals(flag, "1")) {

            if (order != null) {
                if (order.size() > 0) {
                    scidList = new StringBuffer();
                    for (int i = 0; i < order.size(); i++) {
                        OrderPreperBean bean = order.get(i);
                        //如果是从购物车过来的 就取出scid 用,号拼接
                        scidList.append(bean.getScid());
                        if (i != order.size() - 1) {
                            scidList.append(",");
                        }

                    }
                }
//                initView();
            }
        } else {
            //其他全是立即购买
            if (order != null) {
                if (TextUtils.equals(flag, "2")) {
                    buyNum = String.valueOf(order.get(0).getNumber());
                    productAttributeId = order.get(0).getAttributes().getId();
                }
//                initView();
            }
        }
    }



    private void initAddressView() {
        if (TextUtils.isEmpty(addressid)) {
            //登录没有默认地址 就隐藏名字跟手机号
            addressNameLayout.setVisibility(View.GONE);
            detailAddressText.setText("请选择收货地址");
        } else {
            //否则就显示
            addressNameLayout.setVisibility(View.VISIBLE);
            nameLable.setText(addressName);
            phoneLable.setText(addressPhone);
            detailAddressText.setText(addressprovince + detailAddress);
        }
    }

//    private void initView() {
//
//        addressUserInfoLayout.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(OrderPreperActivity.this, Delivery_address.class);
//                intent.putExtra("flag", 1);
//                startActivityForResult(intent, GETADDRESSINFOCODE);
//            }
//        });
//
//        if (order != null) {
//            OrderPreperListAdapter adapter = new OrderPreperListAdapter(this);
//            preperOrderList.setAdapter(adapter);
//            adapter.initAdd(order);
//
//            float price = 0;
//            if (order.size() > 0) {
//                scidList = new StringBuffer();
//                for (int i = 0; i < order.size(); i++) {
//                    OrderPreperBean bean = order.get(i);
//                    float attrprice = Float.valueOf(bean.getAttributes().getPrice());
//                    price = price + (attrprice * bean.getNumber());
//
//                    if (TextUtils.equals(flag, "1")) {
//                        //如果是从购物车过来的 就取出scid 用,号拼接
//                        scidList.append(bean.getScid());
//                        if (i != order.size() - 1) {
//                            scidList.append(",");
//                        }
//
//                    }
//                }
//            }
//            countPrice.setText("￥" + BaseUtils.formatFloat(price));
//            bottomcountprice.setText("￥" + BaseUtils.formatFloat(price));
//
//
//            int fullPostPrice = PreferencesUtils.getIntPreference(this, prefreceName, "fullPostPrice", 0);//满邮价格
//            String postageTip = PreferencesUtils.getStringPreference(this, prefreceName, "postageTip", "");//邮费提示信息
//            String singleIPostPrice = PreferencesUtils.getStringPreference(this, prefreceName, "singleIPostPrice", ""); // 邮费
//
//            if (price >= fullPostPrice) {
//                kuaidilable.setText("快递免邮费");
//                kuaidipricelable.setText("￥0");
//            } else {
//                kuaidilable.setText(postageTip);
//                kuaidipricelable.setText("￥" + singleIPostPrice);
//            }
//
//        }
//    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case RESULT_OK:
                if (requestCode == GETADDRESSINFOCODE) {
                    Bundle b = data.getExtras(); //data为B中回传的Intent
                    Address address = (Address) b.getSerializable("address");//str即为回传的值
                    if (address != null) {
                        changeAddress(address);
                    }
                }
                break;
            default:
                break;
        }
    }

    //改变选择的地址信息
    private void changeAddress(Address as) {
        addressid = as.getId();
        addressName = as.getUser_name();
//                        addressprovince = as.get

        addressprovince = as.getCityId();
        detailAddress = as.getAddress();
        addressPhone = as.getPhone();
        initAddressView();
    }


    @OnClick(R.id.postBtn)
    public void OnPostBtn(View v) {
        userid = PreferencesUtils.getStringPreference(this, prefreceName, prefrece_userid, ""); //获取用户id没有登陆就是没有
        hasPayPassword = PreferencesUtils.getBooleanPreference(this, prefreceName, prefreceKey, false); //获取是否设置过支付密码
        if (TextUtils.isEmpty(userid)) {
            //用户ID为空 跳到登录界面
            startActivity(new Intent(this, user_enter.class));
        } else {

            if (hasPayPassword) {
                if (!TextUtils.isEmpty(addressid)) {
                    postOrder();
                } else {
                    Toast.makeText(this, "请选择收货地址", Toast.LENGTH_SHORT).show();
                }
            } else {
                pass = new dialog_setup_password(OrderPreperActivity.this);
                pass.setOnMenuClick(menuClick);
                pass.showPopupWindow();
            }

        }
    }


    //跳转到订单详情页面
    public void redirectOrderInfo() {
        Intent it = new Intent(OrderPreperActivity.this, OrderInfoActivity.class);
        it.putExtra(OrderInfoActivity.EXTRA_ORDER_ID, orderid);
        startActivity(it);
    }


    /*设置支付密码按钮的回调*/
    dialog_setup_password.OnMenuClick menuClick = new dialog_setup_password.OnMenuClick() {

        @Override
        public void onDoneClick(String pass1, String pass2) {
            postSetupPassword(pass1, pass2);
        }

        @Override
        public void onCancelClick() {
        }
    };


    /*提交订单*/
    private Upload up;

    /*提交订单*/
    private void postOrder() {

        if (!BaseUtils.isNetworkAvailable(this)) {
            //没有网络显示无网络
            Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
            return;
        }


        Map<String, String> params = new HashMap<>();
        params.put("flag", flag);    //从那个页面 从购物车还是立即购买
        params.put("userId", userid);   //手机号
        params.put("addressId", addressid);
//        params.put("ifUseGrouthBei ", false +"");
        if (TextUtils.equals(flag, "1")) {
            params.put("scidlist", scidList.toString());
        } else {
            params.put("productAttributeId", productAttributeId);
            params.put("buyNum", buyNum);
        }


        ApiService.getInstance(this).postOrder(params)
                //涉及到IO操作
                .subscribeOn(Schedulers.io())
                .compose(this.<BaseParse<BaseParse.properOrder>>bindUntilEvent(ActivityEvent.DESTROY)) //跟生命周期绑定一起
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        //开始监听前的处理，一般用于显示dialog什么的
////                        if (hud != null) {
////                            hud.show();
////                        }
                        if (up != null) {
                            up.show();
                        }
                        Log.d("accept", "accept");
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseParse<BaseParse.properOrder>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        //可以取消订阅
                        //还可以判断是否处于取消状态
                        //boolean b=d.isDisposed();

                        Log.d("onSubscribe", "onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull BaseParse<BaseParse.properOrder> s) {
                        //处理得到的数据
                        //订单生成完毕后弹出键盘输入密码框 隐藏下方选择方式
                        if (s.success) {


                            test = new dialog_select_pay_type_test(OrderPreperActivity.this, getSupportFragmentManager());
                            if (s.data.waitPayPrice != null) {
                                float waitPrice = Float.parseFloat(s.data.waitPayPrice);
//                                    s.data.waitPayPrice
                                test.setPrice(waitPrice);
                            }
                            test.setOnPayTypeClick(typeClick);    //调用付款以前一定要先设置payurl
                            test.setOnKeyBordCallBackListener(keybordListener);
                            test.showPopupWindow();
                            test.setOnDismissListener(new BasePopupWindow.OnDismissListener() {
                                @Override
                                public void onDismiss() {
                                    redirectOrderInfo();
                                    //跳转到订单详情 关闭本页面
                                    OrderPreperActivity.this.finish();
                                }
                            });

                            orderid = s.data.orderId;

                        } else {


                            if (s.code == 314) {
                                Toast.makeText(AppApplication.getContext(), s.message, Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(OrderPreperActivity.this, user_enter.class));
                            } else {
                                Toast.makeText(OrderPreperActivity.this, s.message, Toast.LENGTH_SHORT).show();
                            }
                        }
//                        Log.e("quick_login", s);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        //异常处理
                        e.printStackTrace();
                        if (e != null && e.getMessage() != null) {

                            Toast.makeText(OrderPreperActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                        if (up != null && up.isShowing()) {

                            up.dismiss();
                        }
                    }

                    @Override
                    public void onComplete() {
                        //监听完毕后的处理

                        if (up != null && up.isShowing()) {

                            up.dismiss();
                        }
                    }
                });
    }


    //付款dialog的回调
    dialog_select_pay_type_test.KeybordListener keybordListener = new dialog_select_pay_type_test.KeybordListener() {

        @Override
        public void onInputCompleted(CharSequence password) {

            //执行验余额支付的接口会返回错误字符
            postBanlancePay(password.toString());

        }

        @Override
        public void onInputCompleted(CharSequence password, String ordereid) {

        }

        @Override
        public void onForgetPassword() {

        }

        @Override
        public void onPasswordCorrectly() {
            redirectOrderInfo();
            OrderPreperActivity.this.finish();
        }

        @Override
        public void onCancel() {
            redirectOrderInfo();
            //跳转到订单详情 关闭本页面
            OrderPreperActivity.this.finish();
        }

    };

    /*余额付款*/
    private void postBanlancePay(String pay) {
        Map<String, String> params = new HashMap<>();
        params.put("userId", userid);   //手机号
        params.put("orderId", orderid);
        params.put("payType", paytype);
        params.put("payPassword", BaseUtils.EncryptedPassword(pay));

        ApiService.getInstance(this).payBalance(params)
                //涉及到IO操作
                .subscribeOn(Schedulers.io())
                .compose(this.<ParentBean>bindUntilEvent(ActivityEvent.DESTROY)) //跟生命周期绑定一起
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        //开始监听前的处理，一般用于显示dialog什么的
////                        if (hud != null) {
////                            hud.show();
////                        }

                        Log.d("accept", "accept");
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ParentBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        //可以取消订阅
                        //还可以判断是否处于取消状态
                        //boolean b=d.isDisposed();
                        Log.d("onSubscribe", "onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull ParentBean s) {
                        //处理得到的数据
                        Toast.makeText(OrderPreperActivity.this, s.getMessage(), Toast.LENGTH_SHORT).show();

                        if (pass != null) {
                            pass.dismiss();
                        }
                        if (s.isSuccess()) {
                            test.setkeyPadStatus(true);
                        } else {
                            test.setkeyPadStatus(false, s.getMessage());
                        }
                        Log.e("quick_login", s.getMessage());
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        //异常处理
                        e.printStackTrace();
                        if (e != null && e.getMessage() != null) {

                            test.setkeyPadStatus(false, e.getMessage());
                        }
//                        Log.e("onError", e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        //监听完毕后的处理
                        Log.d("onComplete", "onComplete");
                    }
                });
    }


    /*设置支付密码*/
    private void postSetupPassword(String pass1, String pass2) {

        Map<String, String> params = new HashMap<>();
        params.put("payPassword", pass1);   //手机号
        params.put("comfirm_payPassword", pass2);
        params.put("userId", userid);

        ApiService.getInstance(this).setupPayPass(params)
                //涉及到IO操作
                .subscribeOn(Schedulers.io())
                .compose(this.<ParentBean>bindUntilEvent(ActivityEvent.DESTROY)) //跟生命周期绑定一起
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        //开始监听前的处理，一般用于显示dialog什么的
////                        if (hud != null) {
////                            hud.show();
////                        }

                        Log.d("accept", "accept");
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ParentBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        //可以取消订阅
                        //还可以判断是否处于取消状态
                        //boolean b=d.isDisposed();

                        Log.d("onSubscribe", "onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull ParentBean s) {
                        //处理得到的数据
                        Toast.makeText(OrderPreperActivity.this, s.getMessage(), Toast.LENGTH_SHORT).show();

                        if (pass != null) {
                            pass.dismiss();
                        }
                        if (s.isSuccess()) {
                            //本地重置他已经设置过了支付密码
                            PreferencesUtils.setBooleanPreference(AppApplication.getContext(), prefreceName, prefreceKey, true);
                            hasPayPassword = true;
                        }
                        Log.e("quick_login", s.getMessage());
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        //异常处理
                    }

                    @Override
                    public void onComplete() {
                        //监听完毕后的处理
                        Log.d("onComplete", "onComplete");
                    }
                });
    }


    dialog_select_pay_type_test.PayTypeClick typeClick = new dialog_select_pay_type_test.PayTypeClick() {
        @Override
        public void PayAli(String orderid) {

            userid = PreferencesUtils.getStringPreference(AppApplication.getContext(), prefreceName, prefrece_userid, ""); //
            String userno = PreferencesUtils.getStringPreference(AppApplication.getContext(), prefreceName, prefrece_userno, ""); //

            Map<String, String> params = new HashMap<>();
            params.put("userno", userno);
            params.put("userId", userid);
            params.put("orderId", orderid);

            ModelFilteredFactory.compose(RetrofitFactory.getInstance().API().createAliPay(params))
                    .subscribe(
                            new Observer<BaseParse<BaseParse.properOrder>>() {
                                @Override
                                public void onSubscribe(@NonNull Disposable d) {
                                    //可以取消订阅
                                    //还可以判断是否处于取消状态
                                    //boolean b=d.isDisposed();

                                    Log.d("onSubscribe", "onSubscribe");
                                }

                                @Override
                                public void onNext(@NonNull BaseParse<BaseParse.properOrder> s) {
                                    //处理得到的数据
                                    Toast.makeText(OrderPreperActivity.this, s.message, Toast.LENGTH_SHORT).show();
                                    if (s.data != null && s.data.payAliUrl != null) {
                                        startPayAli(s.data.payAliUrl);
                                    } else {
                                        if (BuildConfig.DEBUG) {
                                            Toaster.showOneToast("获取服务器aliUrl失败，该窗口只会在开发模式中显示");
                                        }
                                    }
                                    Log.e("quick_login", s.message);
                                }

                                @Override
                                public void onError(@NonNull Throwable e) {
                                    //异常处理
                                    e.printStackTrace();
                                }

                                @Override
                                public void onComplete() {
                                    //监听完毕后的处理
                                    Log.d("onComplete", "onComplete");
                                }
                            }
                    );

        }

        @Override
        public void PayWechat(String orderID) {

        }
    };

    private void startPayAli(final String url) {
        Runnable payRunnable = new Runnable() {

            @Override
            public void run() {
                PayTask alipay = new PayTask(OrderPreperActivity.this);
                Map<String, String> result = alipay.payV2(url, true);
                Log.i("msp", result.toString());

                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = result;
                mHandler.sendMessage(msg);
            }
        };

        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }

    private static final int SDK_PAY_FLAG = 1;

    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult(
                            (Map<String, String>) msg.obj);
                    /**
                     * 对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    Log.e("my_wallet", "resultInfo:" + resultInfo);
                    Log.e("my_wallet", "resultStatus:" + resultStatus);
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        Toast.makeText(OrderPreperActivity.this, "付款成功",
                                Toast.LENGTH_SHORT).show();
                        //同步回调服务器
//                        returnPayUrl(resultInfo);
                        OrderPreperActivity.this.finish();


                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        Toast.makeText(OrderPreperActivity.this, "付款失败",
                                Toast.LENGTH_SHORT).show();
                    }
                    break;
                }
                default:
                    up.dismiss();
                    break;
            }
        }

        ;
    };


    private void returnPayUrl(String payurl) {
        Map<String, String> params = new HashMap<>();
        params.put("params", payurl);
        ModelFilteredFactory.compose(RetrofitFactory.getInstance().API().payReturnUrl(params))
                .subscribe(
                        new Observer<String>() {
                            @Override
                            public void onSubscribe(@NonNull Disposable d) {
                                //可以取消订阅
                                //还可以判断是否处于取消状态
                                //boolean b=d.isDisposed();
                            }

                            @Override
                            public void onNext(@NonNull String s) {
                                //处理得到的数据
                            }

                            @Override
                            public void onError(@NonNull Throwable e) {
                                //异常处理
                            }

                            @Override
                            public void onComplete() {
                                //监听完毕后的处理
                            }
                        }
                );
    }
}
